import json
import time

import requests
from typing import Dict, List

from coingecko.api.base import CoinGeckoAPI
import BiAn

# SOCKS5 代理配置
proxies = {
    'http': 'socks5://127.0.0.1:10010',
    'https': 'socks5://127.0.0.1:10010',
}

session = requests.Session()
session.proxies = proxies
cg = CoinGeckoAPI("CG-ts8putiaLQt9sMaj4SgVVZY4", session=session)

def get_shizhi(sybol: str):
    # 获取 BTC 的流通供应量
    btc_data = cg.get_data(
        endpoint=f"coins/{sybol}",
        params={"localization": "false", "tickers": "false", "market_data": "true"}
    )

    # 方法2：获取市场数据（原get_coins_markets）
    market_data = cg.get_data(
        endpoint="coins/markets",
        params={"vs_currency": "usd", "ids": sybol}
    )

    # 打印流通量
    print(f"{sybol}流通量: {btc_data['market_data']['circulating_supply']}")
    print(f"{sybol}当前价格: ${market_data[0]['current_price']}")
    sz = btc_data['market_data']['circulating_supply'] * market_data[0]['current_price']
    return sz

# 使用示例
if __name__ == "__main__":
    contracts = ["BTCUSDT", "ETHUSDT", "SOLUSDT"]
    all_coins = cg.get_data(endpoint="coins/list")
    # print(all_coins)
    for coin in all_coins:
        coin['symbol'] = coin['symbol'].upper()
    symbols = BiAn.get_all_futures_symbols()
    select_items = []
    for symbol in symbols:
        symbol = symbol.replace("USDT", "")
        for coin in all_coins:
            if coin['symbol'] == symbol:
                item = {
                    "symbol": symbol,
                    "coin": coin['id']
                }
                select_items.append(item)
    print(len(select_items))
    # print(select_items)
    data = []
    cnt = 0
    for item in select_items:
        # cnt += 1
        # if cnt > 10:
        #     break
        print(item)
        try:
            sz = get_shizhi(item["coin"])
            print(f"{item['symbol']}市值：{sz / 100000000:,.2f}")
            data.append({
                "symbol": item['symbol'],
                "coin": item['coin'],
                "shizhi": sz
            })
            time.sleep(2)
        except  Exception as e:
            print(f"❌ 错误: {str(e)}")

    print(len(data))
    with open("usdt.json", "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=4)
